package cn.xspace.wnx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;

import com.xspace.android.xafcommon.base.BaseActivity;
import com.xspace.android.xafcommon.base.BaseFragment;
import com.xspace.android.xafcommon.util.LogUtil;

import cn.xspace.library.bean.Type;
import cn.xspace.library.util.BldNavigation;
import cn.xspace.library.util.MsgType;
import cn.xspace.wnx.R;
import cn.xspace.wnx.db.ChatRecordDao;
import cn.xspace.wnx.db.MessageRecordDao;
import cn.xspace.wnx.fragment.ChatFragment;
import de.greenrobot.event.EventBus;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by liuchen on 2016/10/12.
 */

public class ChatActivity extends BaseActivity {
    public static ChatActivity chatActivity;
    private BaseFragment chatFragment;
    public static String TAG = ChatActivity.class.getSimpleName();
    //    MessageEntity messageEntity;
    public boolean isVisible;
    public static String keyId = ""; //群组时group_id 单聊是sender_id

    private static int chatType = MsgType.CHAT_SINGLE.toNumber();
    private String name = "";
    private String logo = "";
    private String sex = "1";
    public static int groupType = Type.GroupType.TEMP_GROUP;
    public boolean isJustRecord = false;

    public static String formId = "";
    private String formUrl = "";
    private String formName = "";
    private boolean formIsCantalk = false;
    private int formType = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isVisible = true;
        chatActivity = this;
    }

    @Override
    protected void getIntentData() {
        super.getIntentData();
        Bundle bundle = getIntent().getBundleExtra(BldNavigation.KEY_BUNDLER);
        if (bundle != null) {
            LogUtil.d(TAG, bundle.toString());
            keyId = bundle.getString(BldNavigation.SENDER_ID);
            chatType = bundle.getInt(BldNavigation.CHAT_TYPE, MsgType.CHAT_SINGLE.toNumber());
            name = bundle.getString(BldNavigation.CHAT_NAME);
            logo = bundle.getString(BldNavigation.CHAT_SENDER_LOGO);
            sex = bundle.getString(BldNavigation.CHAT_SENDER_SEX);
            groupType = bundle.getInt(BldNavigation.GROUP_TYPE);
            isJustRecord = bundle.getBoolean(ChatFragment.PAGE_TYPE, false);

            formId = bundle.getString(BldNavigation.FormId);
            formName = bundle.getString(BldNavigation.FormName);
            formType = bundle.getInt(BldNavigation.FormType);
            formUrl = bundle.getString(BldNavigation.FormUrl);
            formIsCantalk = bundle.getBoolean(BldNavigation.FormCanTAlk);
        }
        LogUtil.d(TAG, "keyId>>" + keyId + "chatType>>" + chatType + "name>>" + name);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_chat);
    }

    @Override
    protected void initData() {
//        String id = "";
//        if (chatType == MessageEntity.MsgType.CHAT_SINGLE.toNumber()) {
//            id = messageEntity.msg_sender_id;
//        } else {
//            id = messageEntity.msg_group_id;
//        }

        Bundle args = new Bundle();
        args.putInt(BldNavigation.CHAT_TYPE, chatType);
        args.putString(BldNavigation.SENDER_ID, keyId);
        args.putString(BldNavigation.CHAT_NAME, name);
        args.putString(BldNavigation.CHAT_SENDER_LOGO, logo);
        args.putString(BldNavigation.CHAT_SENDER_SEX, sex);
        args.putInt(BldNavigation.GROUP_TYPE, groupType);
        args.putBoolean(ChatFragment.PAGE_TYPE, isJustRecord);

        args.putString(BldNavigation.FormId, formId);
        args.putString(BldNavigation.FormName, formName);
        args.putInt(BldNavigation.FormType, formType);
        args.putString(BldNavigation.FormUrl, formUrl);
        args.putBoolean(BldNavigation.FormCanTAlk, formIsCantalk);
        chatFragment = ChatFragment.getInstance(args);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        EventBus.getDefault().register(chatFragment);
        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
    }

    @Override
    public void onResume() {
        super.onResume();
        isVisible = true;
        if (chatType != MsgType.CHAT_QAC_FORM.toNumber()) {
            registerSubscription(MessageRecordDao.getInstance().getiMessageData().updateUnreadStatus(keyId, chatType)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe());
        } else {
            registerSubscription(ChatRecordDao.getInstance().getIChatRecordOperation().clearChatRecordUnreadCount(keyId, formId)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe());
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        isVisible = false;
    }

    @Override
    protected void onDestroy() {
        //销毁界面时置空
//        keyId = "";
//        chatActivity = null;
        formId = "";
        super.onDestroy();
    }

    @Override
    protected void onNewIntent(Intent intent) {

        LogUtil.d("onNewIntent" + intent);
        Bundle bundle = getIntent().getBundleExtra(BldNavigation.KEY_BUNDLER);
        if (bundle != null) {
            LogUtil.d(TAG, bundle.toString());
            String mKeyId = bundle.getString(BldNavigation.SENDER_ID);
            int mChatType = bundle.getInt(BldNavigation.CHAT_TYPE, MsgType.CHAT_SINGLE.toNumber());
            if (mChatType == chatType && TextUtils.equals(mKeyId, keyId)) {
                finish();
                startActivity(intent);
            } else {
                super.onNewIntent(intent);
            }
        }
    }

    @Override
    protected boolean isShouldHideKeyboard(View v, MotionEvent event) {
        return false;
    }

    /**
     * 获取当前界面的keyId,当前正在聊天的人的id
     *
     * @return
     */
    public static String getCurrentKeyId() {
        return keyId;
    }

}
